package com.ruoyi.web.controller.goodsStore;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.goodsStore.domain.GoodsStore;
import com.ruoyi.goodsStore.service.IGoodsStoreService;
import com.ruoyi.warehouseout.domain.WareoutOreder;
import com.ruoyi.warehouseout.domain.outGoods;
import com.ruoyi.warehouseout.service.IWareoutOrederService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 商品存储Controller
 * 
 * @author ruoyi
 * @date 2024-11-01
 */
@RestController
@RequestMapping("/goodsStore/store")
public class GoodsStoreController extends BaseController
{
    @Autowired
    private IGoodsStoreService goodsStoreService;
    @Autowired
    private IWareoutOrederService iWareoutOrederService;

    /**
     * 查询商品存储列表
     */
    @PreAuthorize("@ss.hasPermi('goodsStore:store:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsStore goodsStore)
    {
        startPage();
        List<GoodsStore> list = goodsStoreService.selectGoodsStoreList(goodsStore);
        return getDataTable(list);
    }

    @GetMapping("/select")
    public TableDataInfo select(GoodsStore goodsStore){
        startPage();
        List<GoodsStore> list = goodsStoreService.select(goodsStore);
        return getDataTable(list);
    }

    @GetMapping("/selectup")
    public TableDataInfo selectup(GoodsStore goodsStore){
        startPage();
        List<GoodsStore> list = goodsStoreService.selectup(goodsStore);
        return getDataTable(list);
    }

    /**
     * 导出商品存储列表
     */
    @PreAuthorize("@ss.hasPermi('goodsStore:store:export')")
    @Log(title = "商品存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsStore goodsStore)
    {
        List<GoodsStore> list = goodsStoreService.selectGoodsStoreList(goodsStore);
        ExcelUtil<GoodsStore> util = new ExcelUtil<GoodsStore>(GoodsStore.class);
        util.exportExcel(response, list, "商品存储数据");
    }

    /**
     * 获取商品存储详细信息
     */
    @PreAuthorize("@ss.hasPermi('goodsStore:store:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return success(goodsStoreService.selectGoodsStoreByGoodsId(goodsId));
    }

    /**
     * 新增商品存储
     */
    @PreAuthorize("@ss.hasPermi('goodsStore:store:add')")
    @Log(title = "商品存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsStore goodsStore)
    {
        goodsStore.setGoodsNo(SeqNumberUtils.getCPBHNum());
        return toAjax(goodsStoreService.insertGoodsStore(goodsStore));
    }

    /**
     * 修改商品存储
     */
    @PreAuthorize("@ss.hasPermi('goodsStore:store:edit')")
    @Log(title = "商品存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsStore goodsStore)
    {
        return toAjax(goodsStoreService.updateGoodsStore(goodsStore));
    }

    /**
     * 删除商品存储
     */
    @PreAuthorize("@ss.hasPermi('goodsStore:store:remove')")
    @Log(title = "商品存储", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(goodsStoreService.deleteGoodsStoreByGoodsIds(goodsIds));
    }

    /**
     * 修改出库货架状态
     * @param payload
     * @return
     */
    @PostMapping("/updateLocalId")
    public AjaxResult updateLocalId(@RequestBody Map<String, String> payload){
        String wareoutNo = payload.get("wareoutNo");
        List<WareoutOreder> wareoutOreders = iWareoutOrederService.selectWareoutOrederGoodsList(wareoutNo);
        for ( WareoutOreder wareoutOreder : wareoutOreders ){
            for (com.ruoyi.warehouseout.domain.outGoods goods : wareoutOreder.getGoods()){
                System.out.println(goods);
                goodsStoreService.updateLocalIdListByGoodsId(goods.getGoodsId(), Math.toIntExact(goods.getOutboundNumber()));
            }
        }
        return toAjax(1);
    }
}
